#include "stdafx.h"
#include "TestResult.h"

namespace JHCPP{
	namespace CppUnit{
		CTestResult::CTestResult()
		: m_syncObject(new CSynchronizationObject()),
		m_runTests(0),
		m_stop(false)
		{
		}

		CTestResult::~CTestResult()
		{
			std::vector<CTestFailure*>::iterator it;

			for (it = m_errors.begin(); it != m_errors.end(); ++it)
				delete *it;

			for (it = m_failures.begin(); it != m_failures.end(); ++it)
				delete *it;

			delete m_syncObject;
		}

		void CTestResult::addError(CTest* test, CCppUnitException* e)
		{
			CExclusiveZone zone(m_syncObject); 
			m_errors.push_back(new CTestFailure(test, e));
		}

		void CTestResult::addFailure(CTest* test, CCppUnitException* e)
		{
			CExclusiveZone zone(m_syncObject); 
			m_failures.push_back(new CTestFailure(test, e));
		}

		void CTestResult::startTest(CTest* test)
		{
			CExclusiveZone zone(m_syncObject); 
			m_runTests++;
		}

		void CTestResult::endTest(CTest* test)
		{
			CExclusiveZone zone(m_syncObject);
		}

		int CTestResult::runTests()
		{
			CExclusiveZone zone(m_syncObject); 
			return m_runTests;
		}

		int CTestResult::testErrors()
		{
			CExclusiveZone zone(m_syncObject); 
			return (int) m_errors.size();
		}

		int CTestResult::testFailures()
		{
			CExclusiveZone zone(m_syncObject); 
			return (int) m_failures.size();
		}

		bool CTestResult::wasSuccessful()
		{
			CExclusiveZone zone(m_syncObject); 
			return m_failures.size() == 0 && m_errors.size () == 0; 
		}

		std::vector<CTestFailure*>& CTestResult::errors()
		{
			CExclusiveZone zone(m_syncObject); 
			return m_errors;
		}

		std::vector<CTestFailure*>& CTestResult::failures()
		{
			CExclusiveZone zone(m_syncObject); 
			return m_failures;
		}

		bool CTestResult::shouldStop()
		{
			CExclusiveZone zone(m_syncObject); 
			return m_stop;
		}

		void CTestResult::stop()
		{
			CExclusiveZone zone(m_syncObject); 
			m_stop = true;
		}

		void CTestResult::setSynchronizationObject(CSynchronizationObject* syncObject)
		{
			delete m_syncObject; 
			m_syncObject = syncObject;
		}

	}//end of namespace CppUnit
}//end of namespace JHCPP
